#ifndef __DBACCOUNT_LOGINRECIEVER_H__
#define __DBACCOUNT_LOGINRECIEVER_H__

#include "gwmessagetab.hpp"
#include "IMessageOperator.h"
#include "protonet.hpp"
#include "../DBServerTab.h"
#include "sqlquery/QueryAccountInfo.h"
#include "../Sender/DBAccount_Login_FailedSender.h"
#include "../Sender/DBAccount_Login_SuccessSender.h"

/**
*@file       DBAccount_LoginReciever.h
*@brief      process message DBAccount_Login
*
*@author     graywind
*@version    1.0.0.0
*@data       2012/3/27
*/

///process message
class OnDBAccount_Login
{
public:
    ///@brief    process message
    ///param[in] op            operator type
    ///param[in] session_id    identifier of session
    ///param[in] pMsgBuffer    message buffer
    ///param[in] msg_bytes     byte length of message buffer
    ///retval    void
    static void OnMapping(IMessageOperator* op, int session_id, void* pMsgBuffer, unsigned long msg_bytes)
    {
        //TODO
        AccountInfo param;
        if(ProtoNet<AccountInfo>::CreateFromBytes(param, pMsgBuffer, msg_bytes))
        {
            QueryAccountInfo::stmt->setString(1, param.usrname());
            QueryAccountInfo::stmt->setString(2, param.usrpassword());

            if( QueryAccountInfo::stmt->execute())
            {
                sql::ResultSet* result = QueryAccountInfo::stmt->getResultSet();
			    if(result->next())
			    {
                    int id = result->getInt(1);

				    AccountDigestInfo udi;
                    udi.set_usrid(id);
                    udi.set_logincode(param.logincode());

                    if(!result->isNull(2))
                    {
                        do
                        {
                            sql::SQLString role_code = result->getString(2);
                            sql::SQLString role_name = result->getString(3);
                            int level = result->getInt(4);
                            int job = result->getInt(5);
                            int gender = result->getInt(6);
                            int graphics = result->getInt(7);
                            int world_id = result->getInt(8);

                            RoleInfoDigest* digest = udi.add_roles();
                            digest->set_rolecode(role_code);
                            digest->set_rolename(role_name);
                            digest->set_rolelevel(level);
                            digest->set_jobtype(job);
                            digest->set_worldid(world_id);
                            digest->set_gender(gender);
                            digest->set_graphicsid(graphics);
                        }
                        while(result->next() && !result->isNull(2));
                    }
                    
                    msgDBAccount_Login_Success msg(session_id, &udi);
                    gwMessagePort::ProcessMessage(&msg);
                }
			    else
			    {
                    msgDBAccount_Login_Failed msg(session_id, LoginFailed::Account_Or_Password_Not_Match, param.logincode());
                    gwMessagePort::ProcessMessage(&msg);
			    }
            }
            QueryAccountInfo::stmt->clearParameters();
        }
    }
};

///[message group]  [message type]  [function table] [handler]
RegisterMessageTab(DBServerTypes, DBAccount_Login, DBServerTab, OnDBAccount_Login);
#endif
